Docker
Container
Basic
docker run <container> to run a container.
-dDetached mode 后台模式-p <host/mac port>:<container port>Mapping network ports--name <name>Give the container a name-it <OS>streaming interactive mode 交互模式docker psto see running and stopped (-a) container
docker imagesto see info about images
e.g. docker run hello-world does following things:
- The Docker client send command (API) to deamon
- The Docker daemon will search the container locally
- If not found locally, deamon will pull the “hello-world” image from Docker Hub
- Daemon created a new container from that image, run it
- Daemon streamed that output to the Docker client, which sent it to your terminal
- If the container has done the job, it will exit by default

docker start <container> 启动某个停止的container
docker stop <container> 停止某个运行的container (data keeps)
docker rm <container> 删除某个container (data erasing)
Images
Images is stopped container
Container is running images
Basic
一般 docker run 会自动下载image 并且启动container,但是我们也可以单纯下载image 而不启动container
docker pull container 下载某个images
docker images 显示所有images
docker rmi images 删除某个image
Layers
The final working image usually been comprised of mutiple layered images.

Union Mount
越顶层的内容会覆盖下面layers 的内容,所以当我们需要修改任何内容的时候,我们不需要看到所有底层的内容,只需要关注顶层。
这个顶层是通过Union Mount 的形式将系统外的文件实时映射到container 内部中。
New Images
docker commit <container> <repo:tag> create a new image from a container’s changes.
## e.g.
# 1. we made a lot of change on a container test
# 2. find container's id (abc123456def)
docker ps -a
# 3. create a new image called testImage
# if we use dockerfile we dont need this step
docker commit abc123456def testImage:1.0
# 4. find out the histrical opreations
docker history testImage
# 5. login to docker hub or other registry
docker login --username=jisi7241
# or
docker login registry.gitlab.com
# 6. tag your new committed images
docker tag abc123456def jisi7241/testImage:firsttry
# 7. push it to the registry
docker push jisi7241/testImage
Dockerfile
A file that used to create docker images, bettere than just docker commit
Dockerfile needs to be placed in the top of your project directory, but never put it in your system root!
## Unbuntu based Hello World container
## The base image
FROM ubuntu:15.04
## Contact Info
MAINTAINER jisi724@gmail.com
## Run command against the base layer
## Every RUN command create a new layer, commit the new layer, and run next command based on this layer
## 10 RUN commands, 10 layers
RUN apt-get update
## Command
CMD ["echo", "Hello World"]
RUN command 会新建一个layer 并且在这个基础上在运行下面的命令,所以尽量可能少的用RUN
在Dockerfile 的基础上,我们运行
docker build -t helloworld:0.1 .去build 这个container- -t add a tag
- helloworld:0.1 name of image
- . 在当前文件夹建立这个container
Build Cache是指docker 会把已经build 好的images or layers 进行缓存,如果下次build 用到了相同的layer,就会直接从cache 读取而不会从build
RUN vs CMD
RUN and CMD both work as executing some commands, but they have difference:
- RUN is a build-time command, used to build container, like add layers, install app and so on.
- CMD is a run-time command, use to run commands inside of the container at lunch time.
- We can only put one CMD in the Dockerfile, if we put mutiple CMD, only the last one will be effective.
- ENTRYPOINT is an alternative of CMD, it can’t be overwrited.
ENV
ENV 可以定义在Dockerfile 里作为全局变量使用
FROM ...
...
ENV var1=ping var2=8.8.8.8
CMD $var1 $var2
甚至在SSH 进入container 之后,这些变量依然存在
VOLUMN
Volumn will mapping the outer directory into the container’s directory
docker run -v .outer/data:/inner/data
FROM ubuntu
RUN ...
VOLUMN /data:/data
Build a Webserver
## build a web server with docker
FROM ubuntu:15.04
RUN apt-get update && apt-get install -y \
apache2 \
apache2-utils \
vim \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
EXPOSE 80
ENTRYPOINT ["apache2ctl"]
CMD [-D", "FOREGROUND"]
docker build -t="webServer" .
docker run -d -p 5001:80 --name=web1 webServer
Docker 三部曲
Docker Swarm
Docker 发布的集群式分布工具。
例如单个docker-engine 无法负担APP 所需的性能,那么我们可能需要多个docker-engines 去共同合作运行同一个APP,而用户并不会感知到背后有许多个docker 集群,而会觉得就在一个APP 上操作。
Docker Swarm 分为Manager 和Worker,scalable,extendabel
Issues
Out of disk
docker-machine stop
VBoxManage modifyvm default --cpus 2
VBoxManage modifyvm default --memory 4096
docker-machine start